#include "smart_plc.h"
#include "protocol.h"



struct Local_did_Analy local_did_analy[]=
{
    { _ACK            ,0  ,"本地确认"},
    { _NACK           ,0  ,"本地否认"},
    { CMD_SET_AID     ,4  ,"设置AID"},
    { CMD_GET_AID     ,0  ,"读取AID"},
    { CMD_ACK_AID     ,4  ,"读取AID回复"},
    { CMD_DEL_AID     ,4  ,"删除AID"},
    { CMD_REQ_AID     ,0  ,"设备请求AID"},
    { CMD_GET_SID     ,0  ,"读取SID"},
    { CMD_ACK_SID     ,2  ,"读取SID回复"},
    { CMD_GET_EID     ,0  ,"读取EID"},
    { CMD_ACK_EID     ,8  ,"读取EID回复"},
    { CMD_SET_BPS     ,2  ,"设置波特率"},
    { CMD_SET_REG     ,1  ,"设置注册属性"},
    { CMD_UNLINK      ,0  ,"断开链路"},
    { CMD_REGINFOR    ,4  ,"上报PANID和密码"},
    { CMD_SET_PANID   ,2  ,"设置PANID"},
    { CMD_GET_GWAID   ,4  ,"读取网关ID"},
    { CMD_GET_PANID   ,2  ,"读取PANID"},
    { CMD_ACK         ,0  ,"确认"},
    { CMD_NAK         ,0  ,"否认"},
    { 0xfe            ,0  ,"无法识别的本地did"}
};


//struct Local_did_Analy *get_local_cmd_analy(uint8 cmd)
//{
//    for(uint i=0;i<array_size(local_did_analy);i++)
//    {
//        if(cmd == local_did_analy[i].cmd)
//            return &(local_did_analy[i]);
//    }

//    return &(local_did_analy[array_size(local_did_analy)-1]);
//}

/*功能：根据本地cmd解析该cmd的文字描述
 * 参数：本地cmd/did，解析字符串输出在AnalyStr，该cmd对应的数据域的解析方式输出在dataAnalyType
*/
void get_local_cmd_analy(uint8 cmd, QString &AnalyStr, QString &dataAnalyType)
{
    bool isOK;
    int db_row=0;//先定位到db中某功能码所在的行
    qDebug("报文中的本地did=%X",cmd);
    for(db_row=0;db_row < model_7E_LocalCMD->rowCount();db_row++)
    {
        uint8 cmd_in_db = model_7E_LocalCMD->record(db_row).value("cmd").toString().toInt(&isOK,16);

        //qDebug("数据库中的did=%X",cmd_in_db);
        if( cmd_in_db ==  cmd)
        {
            AnalyStr += QString("<p>本地cmd:<gy> 0x%1 ").arg(cmd,2,16,(QChar)'0')
                    +model_7E_LocalCMD->record(db_row).value("instruction").toString() + "</gy></p>";
            dataAnalyType = model_7E_LocalCMD->record(db_row).value("data_len").toString();
            break;
        }
    }
    if(db_row == model_7E_LocalCMD->rowCount() )
    {
        AnalyStr += "目前不支持该did，在本地CMD维护界面添加后即可支持";
        return ;
    }

}
